<?php

namespace ms\controllers;

use Yii;
use common\models\SysUsers;
use common\models\searchs\SearchSysUsers;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * SysUsersController implements the CRUD actions for SysUsers model.
 */
class SysUsersController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all SysUsers models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new SearchSysUsers();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single SysUsers model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {   
        $sql = "SELECT * from dh_sys_users where id = $id";
        $result = \Yii::$app->db->createCommand($sql);
        $data = $result->queryOne();
//        print_r($data);die();
        return $this->render('view', [
            'model' => $this->findModel($id),
            'data'=>$data,
        ]);
    }

    /**
     * Creates a new SysUsers model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new SysUsers();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing SysUsers model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing SysUsers model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the SysUsers model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return SysUsers the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = SysUsers::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
    public function actionGetbytype() {
        $request = Yii::$app->request;
        $id = $request->get('id');
        $role = $request->get('role');
     
        $model = $this->findModel($id);
        if($role == 3){
            $role = 4;
            $model->role = 4;
            $model->type = 3;
            $model->save();
        }
            
        return $this->redirect(['index']);
    }
    
    public function actionGetusertype(){
        $request = Yii::$app->request;
        $id = $request->get('id');
        $status = $request->get('status');
        $model = $this->findModel($id);
        if($status == 1){
            
            $model->status = 2;
        }else{
           $model->status = 1; 
        }
        $model->save();    
        return $this->redirect(['index']);
    }
    public function actionSetours(){
        $request = Yii::$app->request;
        $id = $request->get('id');
        $status = $request->get('ours');
        $model = $this->findModel($id);
        if($status == 0){
            
            $model->ours = 1;
        }else{
           $model->ours =0; 
        }
        $model->save();    
        return $this->redirect(['user-list']);
    }
    public function actionAudit(){
        $request = Yii::$app->request;
        $id = $request->get('id');
        $type = $request->get('type');
        $nr = $request->get('nr');
        $model = $this->findModel($id);
        $model->type = 2;
        $model->role = 3;
        $model->save();    
        return $this->redirect(['index']);
    }
    
    public function actionFeedback(){
        $request = Yii::$app->request;
        $id = $request->get('id');
        $nr = $request->get('nr');
        $model = $this->findModel($id);
        $model->audit = $nr;
        $model->save();    
        return $this->redirect(['index']);
    }
    
     public function actionUserList(){
        $searchModel = new SearchSysUsers();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('userlist', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
}
    